package cn.vcjf.weapp.api.active.service.impl;

import cn.vcjf.weapp.api.active.entity.GoodsSetmeal;
import cn.vcjf.weapp.api.active.entity.GoodsSetmealDetail;
import cn.vcjf.weapp.api.active.mapper.GoodsSetmealDetailMapper;
import cn.vcjf.weapp.api.active.mapper.GoodsSetmealMapper;
import cn.vcjf.weapp.api.active.service.GoodsSetmealService;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-28
 */
@Service("goodsSetmealService")
public class GoodsSetmealServiceImpl extends ServiceImpl<GoodsSetmealMapper, GoodsSetmeal> implements GoodsSetmealService {

	@Resource
	private GoodsSetmealDetailMapper goodsSetmealDetailMapper;

	@Override
	public GoodsSetmeal selectById(Serializable id){
		GoodsSetmeal goodsSetmeal = baseMapper.selectById(id);

		Wrapper<GoodsSetmealDetail> query = new EntityWrapper<>();
		query.eq("setmealId", goodsSetmeal.getId());
		List<GoodsSetmealDetail> goodsSetmealDetails = goodsSetmealDetailMapper.selectList(query);

		goodsSetmeal.setGoodsSetmealDetailList(goodsSetmealDetails);
		return goodsSetmeal;
	}


	@Override
	public List<GoodsSetmeal> selectList(Wrapper<GoodsSetmeal> wrapper) {
		List<GoodsSetmeal> goodsSetmeals = baseMapper.selectList(wrapper);

		return getGoodsSetmealDetail(goodsSetmeals);
	}

	@Override
	public Page<GoodsSetmeal> selectPage(Page<GoodsSetmeal> page) {
		List<GoodsSetmeal> goodsSetmeals = baseMapper.selectPage(page, Condition.EMPTY);

		goodsSetmeals = getGoodsSetmealDetail(goodsSetmeals);
		return page.setRecords(goodsSetmeals);
	}
	@Override
	public List<GoodsSetmeal> getGoodsSetmealDetail(List<GoodsSetmeal> goodsSetmeals ){
		for (GoodsSetmeal setmeal : goodsSetmeals) {
			Wrapper<GoodsSetmealDetail> query = new EntityWrapper<>();
			query.eq("setmealId", setmeal.getId());
			List<GoodsSetmealDetail> goodsSetmealDetails = goodsSetmealDetailMapper.selectList(query);
			setmeal.setGoodsSetmealDetailList(goodsSetmealDetails);
		}
		return goodsSetmeals;
	}
}
